<?php

namespace app\tours\model;

use think\model\Relation;

class News extends Relation {

        protected $link = array(
            'prov' => array(
                'mapping_type' => BELONGS_TO,
                'class_name' => 'Location',
                'foreign_key' => 'prov',
                'mapping_fields' => 'name as prov_name',
                'as_fields' => 'prov_name',
                'parent_key' => 'id'
            ),
            'city' => array(
                'mapping_type' => BELONGS_TO,
                'class_name' => 'Location',
                'foreign_key' => 'city',
                'mapping_fields' => 'name as city_name',
                'as_fields' => 'city_name',
                'parent_key' => 'id'
            ),
            'scenic' => array(
                'mapping_type' => BELONGS_TO,
                'class_name' => 'Location',
                'foreign_key' => 'scenic',
                'mapping_fields' => 'name as scenic_name',
                'as_fields' => 'scenic_name',
                'parent_key' => 'id'
            ),
        );

        protected function _after_find(&$result, $options = array()) {
                parent::_after_find($result, $options);
                //取关键字替换信息
                if (in_array($result['prov'], array(21, 302, 327, 385))) {
                        $city = M("Location")->field("name,write_string")->where(array("id" => $result['prov']))->find();
                        $result['city_name'] = $city['name'];
                        $result['city_string'] = $city['write_string'];
                } else {
                        $city = M("Location")->field("name,write_string")->where(array("id" => $result['city']))->find();
                        $result['city_name'] = $city['name'];
                        $result['city_string'] = $city['write_string'];
                }
        }

        protected function _after_select(&$resultSet, $options = array()) {
                parent::_after_select($resultSet, $options);
                foreach ($resultSet as $key => $value) {
                        if (!empty($value['city'])) {
                                $city = M("Location")->field("name,write_string")->where(array("id" => $value['city']))->find();
                                $resultSet[$key]['city_name'] = $city['name'];
                                $resultSet[$key]['city_string'] = $city['write_string'];
                        }

                        $resultSet[$key]['url'] = U('/news/' . $value['id'], '', 'html', false, true);

                        if ($value['image_path']) {
                                $resultSet[$key]['image_full'] = C("imgurl") . $value['image_path'];
                        } else {
                                $resultSet[$key]['image_full'] = "/public/tours/images/nophoto.jpg";
                        }
                }
        }

        /**
         * 获取图片新闻  所有带图片的新闻 内容
         * 
         * @create time 2013-4-18
         * @author fishinsky
         */
        public function getPicNews($cid = '') {
                $data['ischecked'] = 1;
                $data['image_path'] = array('neq', '');
                $image_news = null;
                if ($cid) {
                        $data['cid'] = $cid;
                        $image_news = $this->where($data)->limit(10)->order('checkdate desc')->select();
                } else {
                        //图片新闻
                        $image_news = $this->where($data)->limit(10)->order('checkdate desc')->select();
                }
                if (!empty($image_news)) {
                        return $image_news;
                }
                return null;
        }

}
